package interviewClassic150;

import org.junit.Test;

public class Candy {
    public int candy(int[] ratings) {
        //定义两个数组，分别是从左向右遍历的和从右向左遍历的
        int n = ratings.length;
        int[] left = new int[n];
        int[] right = new int[n];
        //初始化
        for (int i = 0; i < n; i++) {
            left[i] = 1;
            right[i] = 1;
        }
        //从左向右
        for (int i = 0; i < n - 1; i++) {
            if (ratings[i] < ratings[i + 1]) left[i + 1] = left[i] + 1;
            if (ratings[n - 2 - i] > ratings[n - 1 - i]) right[n - 2 - i] = right[n - 1 - i] + 1;
        }
        //计算结果
        int res = 0;
        for (int i = 0; i < n; i++) {
            res += Math.max(left[i],right[i]);
        }

        return res;
    }

    @Test
    public void test() {
        int[] ratings = {1,2,2};
        int candy = candy(ratings);
        System.out.println(candy);
    }
}
